package com.ligotop.admin.system.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ligotop.admin.system.entity.Menu;
import com.ligotop.admin.system.entity.User;
import com.ligotop.admin.system.mapper.MenuMapper;
import com.ligotop.admin.system.mapper.RoleMapper;
import com.ligotop.admin.system.mapper.UserMapper;
import com.ligotop.admin.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 *
 * @author yudm
 * @since 2017-09-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserByLoginName(String loginName) {
        User user = baseMapper.selectOne(new User(loginName));
        if (user == null){
            return null;
        }
        Integer userId = user.getId();

        user.setRoles(roleMapper.findListByUserId(userId));
        List<Menu> menuList;

        //超级管理员
        if (User.ADMIN_USER_ID.equals(userId)) {
            menuList = menuMapper.selectList(null);
        } else {
            menuList = menuMapper.findListByUserId(userId);
        }
        user.setMenus(menuList);
        return user;
    }

}
